using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Prejection : MonoBehaviour
{
    private GameObject[] Plants;
    public float t;
    public float level=0;
    private void Start()
    {
        CreatePhysicsScene();
    }
    private Scene _simulationScene;
    private PhysicsScene2D _physicsScene;
    [SerializeField] private Transform[] _obstaclesParents;
    void CreatePhysicsScene()
    {
        _simulationScene = SceneManager.CreateScene("simulation", new CreateSceneParameters(LocalPhysicsMode.Physics2D));
        _physicsScene = _simulationScene.GetPhysicsScene2D();


        AddObj();
    }
    private void Update()
    {
        if(level!=level_manager.ins.level)
        {
            level = level_manager.ins.level;
            Destoryall();
            AddObj();
        }
    }
    void Destoryall()
    {
        var objs = Resources.FindObjectsOfTypeAll<simulateObj>();
        foreach(simulateObj obj in objs)
        {
            obj.gameObject.SetActive(false);
        }
    }
    void AddObj()
    {
        foreach (Transform obj in _obstaclesParents[level_manager.ins.level])
        {
            var ghostObj = Instantiate(obj.gameObject, obj.position, obj.rotation);
            if (ghostObj.GetComponent<Renderer>() != null)
            {
                ghostObj.GetComponent<Renderer>().enabled = false;
            }
            ghostObj.AddComponent<simulateObj>();
            SceneManager.MoveGameObjectToScene(ghostObj, _simulationScene);
        }
    }

    [SerializeField] private LineRenderer _line;
    [SerializeField] private int _maxPhysicsFrameIterations;
    public void SimulateTrajectory(SimpleRocket rocketPrefab,Vector3 pos)
    {
        var ghostObj = Instantiate(rocketPrefab, pos, Quaternion.identity);
        ghostObj.GetComponent<Renderer>().enabled = false;
        SceneManager.MoveGameObjectToScene(ghostObj.gameObject, _simulationScene);

        ghostObj.GetComponent<SimpleRocket>().RocketLaunch(true);
        _line.positionCount = _maxPhysicsFrameIterations;

        for(int i=0;i<_maxPhysicsFrameIterations;i++)
        {
            _physicsScene.Simulate(Time.fixedDeltaTime);
            Plants = GameObject.FindGameObjectsWithTag("Point");
            Transform target = GameObject.Find("target").transform;
            if(Vector3.Distance(ghostObj.transform.position, target.position) <target.GetComponent<Target>().distance)
            {
                ghostObj.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
            }
            else
            {
                for (int j = 0; j < Plants.Length; j++)
                {
                    if (Vector3.Distance(ghostObj.transform.position, Plants[j].transform.position) < Plants[j].GetComponent<Plant>().distance)
                    {
                        Vector3 v1 = Vector3.Normalize(Plants[j].transform.position - ghostObj.transform.position) * (Plants[j].GetComponent<Plant>().gv);
                        Vector2 v2 = new Vector2(v1.x, v1.y);
                        ghostObj.GetComponent<Rigidbody2D>().velocity = ghostObj.GetComponent<Rigidbody2D>().velocity + v2 * Time.deltaTime;
                    }
                }
            }     
                _line.SetPosition(i, ghostObj.transform.position);
        }
        Destroy(ghostObj.gameObject);

    }
}
